package cn.est.controller;


import cn.est.dto.EvaluateDto;
import cn.est.dto.MalfunctionDto;
import cn.est.dto.Result;
import cn.est.dto.UsersDto;
import cn.est.pojo.Evaluate;
import cn.est.service.EvaluateService;
import cn.est.service.MalfunctionService;
import cn.est.service.UsersService;
import cn.est.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评估信息 前端控制器
 * </p>
 *
 * @author qf
 * @since 2021-08-04
 */
@Api("评估信息")
@RestController
@RequestMapping("/api/evaluate")
public class EvaluateController {

    // 故障service
    private final MalfunctionService malfunctionService;
    private final UsersService usersService;
    private final EvaluateService evaluateService;

    public EvaluateController(MalfunctionService malfunctionService, UsersService usersService, EvaluateService evaluateService) {
        this.malfunctionService = malfunctionService;
        this.usersService = usersService;
        this.evaluateService = evaluateService;
    }

    @ApiOperation("specList")
    @GetMapping("specList")
    public Result<List<MalfunctionDto>> findSpecListByModelId(Long modelId){
        List<MalfunctionDto> list=malfunctionService.findSpecListByModelId(modelId);
        return ResultUtils.returnDataSuccess(list);
    }

    @PostMapping("assess")
    public Result<Evaluate> assess(Long modelId, String optionIds, HttpServletRequest request){
        // 1.先获取token
        String token = request.getHeader("token");
        // 2.通过 token 获取用户信息
        UsersDto user = usersService.getLoginByToken(token);

        // 3.调用评估方法，进行评估
        Evaluate evaluate = evaluateService.assess(modelId,optionIds,user.getId());
        return ResultUtils.returnDataSuccess(evaluate);
    }

    @GetMapping("{id}/assessInfo")
    public Result<EvaluateDto> assessInfo(@PathVariable Long id){
        EvaluateDto evaluateDto = evaluateService.assessInfo(id);
        return ResultUtils.returnDataSuccess(evaluateDto);
    }

}

